<?php
class cmsdream_statistics {
	private $table = '';
	
	public function save() {
		$db = $GLOBALS['DB'];
		$_client = $GLOBALS['Q']->client;
		$_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'unknown';
		if($_agent=='unknown') {
			$_client = 'unknown';
		}
		if(preg_match('/(Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla)/i', $_agent)){
			$_client = 'spider';
		}
		/*
		if(preg_match('/(soso)/i', $_agent)){
			$stop = true;
			$_client = 'spider';
		}
		*/
		$GLOBALS['Q']->client = $_client;
		$_date = date("Y-m-d", time());
		
		if($db->fetch_field("SELECT COUNT(*) FROM {$this->table} WHERE `date` = '$_date'") == 0){
			$db->query("INSERT INTO {$this->table}(`date`) VALUES('$_date')");
		}
		$sql = '`web` = `web` + 1';
		if($_client=='wap') {
			$sql = '`wap` = `wap` + 1';
		} else if($_client=='3g') {
			$sql = '`mobile` = `mobile` + 1';
		} else if($_client=='spider') {
			$sql = '`spider` = `spider` + 1';
		}
		$db->query("UPDATE {$this->table} SET `pv` = `pv` + 1, $sql WHERE `date` = '$_date'");
						
		unset($db,$_agent,$_client,$sql);
	}
	
	public function __construct() {
		$this->table = $GLOBALS['C']->TABLE_PREFIX.'statistics';
	}
	
	public function __destruct(){
	
	}
	
}

